﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RayDen.Library.Core.Primitives;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Types;

namespace RayDen.RayEngine.Engines.ProgressivePhotonMapping
{
    public class PPMPhotonPath : PathSamplerBase
    {
        public float Alpha = 0.7f;
        private RgbSpectrum ThroughtPut;
        protected int depth;
        protected SurfaceIntersectionData hitInfo;
        protected RayEngineScene scene;

        public override void InitPath(IPathProcessor buffer)
        {
            base.InitPath(buffer);
            scene = pathIntegrator.Scene;

            this.Sample = new Sample(pathIntegrator.Sampler);
            this.RayIndex = -1;
            float lspdf;

            int li = scene.SampleLights(this.Sample.GetLazyValue(), out lspdf);
            var ls = new LightSample();
            bool done = false;
            while (!done)
            {
                scene.Lights[li].EvaluatePhoton(
                    scene,
                    Sample.GetLazyValue(),
                    Sample.GetLazyValue(),
                    Sample.GetLazyValue(),
                    Sample.GetLazyValue(),
                    Sample.GetLazyValue(),
                    out ls);

                done = ls.Pdf > 0f;

            }

            var pathWeight =  (ls.Pdf *  lspdf);

            this.ThroughtPut = ((RgbSpectrum)(ls.Spectrum) / pathWeight);
            this.PathRay = ls.LightRay;
        }
    }
}
